module ai {
	export class ChessMath extends BaseClass {
		public constructor() {
			super();
		}
		private threshold = 1.15;

		public createArray(w, h) {
			var r = []
			for (var i = 0; i < w; i++) {
				var row = new Array()
				for (var j = 0; j < h; j++) {
					row.push(0)
				}
				r.push(row)
			}
			return r
		}

		public equal(a, b) {
			b = b || 0.01
			return b >= 0 ? ((a >= b / this.threshold) && (a <= b * this.threshold)) : ((a >= b * this.threshold) && (a <= b / this.threshold));
		}

		public greatThan(a, b) {
			return b >= 0 ? (a >= (b + 0.1) * this.threshold) : (a >= (b + 0.1) / this.threshold);// 注意处理b为0的情况，通过加一个0.1 做简单的处理
		}

		public greatOrEqualThan(a, b) {
			return this.equal(a, b) || this.greatThan(a, b)
		}
		public littleThan(a, b) {
			return b >= 0 ? (a <= (b - 0.1) / this.threshold) : (a <= (b - 0.1) * this.threshold)
		}
		public littleOrEqualThan(a, b) {
			return this.equal(a, b) || this.littleThan(a, b)
		}

		public containPoint(arrays, p) {
			for (var i = 0; i < arrays.length; i++) {
				var a = arrays[i]
				if (a[0] === p[0] && a[1] === p[1]) return true
			}
			return false
		}

		public pointEqual(a, b) {
			return a[0] === b[0] && a[1] === b[1]
		}

		public round(score) {
			var neg = score < 0 ? -1 : 1
			var abs = Math.abs(score)
			if (abs <= chess.ChessConst.score.ONE / 2) return 0
			if (abs <= chess.ChessConst.score.TWO / 2 && abs > chess.ChessConst.score.ONE / 2) return neg * chess.ChessConst.score.ONE
			if (abs <= chess.ChessConst.score.THREE / 2 && abs > chess.ChessConst.score.TWO / 2) return neg * chess.ChessConst.score.TWO
			if (abs <= chess.ChessConst.score.THREE * 1.5 && abs > chess.ChessConst.score.THREE / 2) return neg * chess.ChessConst.score.THREE
			if (abs <= chess.ChessConst.score.FOUR / 2 && abs > chess.ChessConst.score.THREE * 1.5) return neg * chess.ChessConst.score.THREE * 2
			if (abs <= chess.ChessConst.score.FIVE / 2 && abs > chess.ChessConst.score.FOUR / 2) return neg * chess.ChessConst.score.FOUR
			return neg * chess.ChessConst.score.FIVE
		}


	}
}